package com.jumi.microservice.rocketmq;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.OrderConsumerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.aliyun.openservices.ons.api.order.MessageOrderListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
public class OrderConsumerClient {

    @Autowired
    private RocketMqConfig mqConfig;

    @Resource
    private CpsUserCashCheckAllListener userCashCheckAllListener;

    @Resource
    private CpsUserCashCheckBatchListener userCashCheckBatchListener;

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public OrderConsumerBean buildOrderConsumer() {
        OrderConsumerBean orderConsumerBean = new OrderConsumerBean();
        //配置文件
        Properties properties = mqConfig.getMqProperties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, mqConfig.getGroup());
        orderConsumerBean.setProperties(properties);
        //订阅关系
        Map<Subscription, MessageOrderListener> subscriptionTable = new HashMap<>();
        Subscription subscription = new Subscription();
        subscription.setTopic("cpsUserCashCheckAll");
        subscriptionTable.put(subscription, userCashCheckAllListener);

        subscription = new Subscription();
        subscription.setTopic("cpsUserCashCheckBatch");
        subscriptionTable.put(subscription, userCashCheckBatchListener);
        //订阅多个topic如上面设置
        orderConsumerBean.setSubscriptionTable(subscriptionTable);
        return orderConsumerBean;
    }

}
